﻿/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Globalization;

namespace DalmatianBuild.Test
{
    public static class AssertB
    {

        public static void ThrowsException(Action action, string messageText = null)
        {
            ThrowsException<Exception>(action, messageText);
        }
        public static void ThrowsException<T>(Action action, string messageText = null)
            where T: Exception
        {
            ThrowsException<T>(action, null, messageText);
        }

        public static void ThrowsException<T>(Action action, Predicate<T> predicate, string messageText = null)
         where T : Exception
        {
            try
            {
                action();
            }
            catch (T e)
            {
                if (predicate == null)
                    return;

                if (!predicate(e))
                {
                    var messageDetail = string.Format(CultureInfo.InvariantCulture, "Exception failed to meet the predicate");
                    if (string.IsNullOrEmpty(messageText))
                        messageText = messageDetail;
                    else
                        messageText = messageText + "\n" + messageDetail;


                    throw new ApplicationException(messageText);
                }
                else
                    return;
            }
            catch (Exception e)
            {
                var messageDetail = string.Format(CultureInfo.InvariantCulture, "Expected Exception <{0}>, actual exception <{1}>", typeof(T).FullName, e.GetType().FullName);
                if (string.IsNullOrEmpty(messageText))
                    messageText = messageDetail;
                else
                    messageText = messageText + "\n" + messageDetail;
                throw new ApplicationException(messageText, e);
            }

            {
                var messageDetail = "Expected Exception <" + typeof(T).FullName + ">, no exception thrown";
                if (string.IsNullOrEmpty(messageText))
                    messageText = messageDetail;
                else
                    messageText = messageText + "\n" + messageDetail;
                Assert.Fail(messageText);
            }
        }


    }
}
